﻿using Chaunce.FrameWork.NetCore.IoC;
using Microsoft.Extensions.Configuration;

namespace Chaunce.FrameWork.NetCore.Helpers
{
    /// <summary>
    /// Config帮助类
    /// </summary>
    public class ConfigHelper
    {
        private static readonly IConfiguration configuration;
        static ConfigHelper()
        {
            configuration = IoCContainer.Resolve<IConfiguration>();
        }
        /// <summary>
        /// 获取节点对象
        /// 嵌套比较深用此方法依次调用GetSection直到获取所需值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IConfigurationSection GetSection(string key)
        {
            return configuration.GetSection(key);
        }

        /// <summary>
        /// 获取指定节点值
        /// NOTE:指定节点下已没有节点，否则返回null
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfigurationValue(string section, string key)
        {
            return GetSection(section)?[key];
        }

        /// <summary>
        /// 获取数据库字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConnectionString(string key)
        {
            return configuration.GetConnectionString(key);
        }
    }
}
